package com.tako;

import javafx.collections.ObservableList;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Service {

    public static File desktop = new File(System.getProperty("user.home") + File.separator + "Desktop");

    public List<MergeTask> addFolder() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        //设置标题
        directoryChooser.setTitle("选择文件夹");
        //设置打开初始地址
        directoryChooser.setInitialDirectory(desktop);
        File file = directoryChooser.showDialog(new Stage());
        return getItemList(new File[]{file});
    }

    public List<MergeTask> batchAddFolder() {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        //设置标题
        directoryChooser.setTitle("选择文件夹");
        //设置打开初始地址
        directoryChooser.setInitialDirectory(desktop);
        File file = directoryChooser.showDialog(new Stage());
        File[] files = file.listFiles(File::isDirectory);
        return getItemList(files);
    }

    private List<MergeTask> getItemList(File[] files) {
        List<MergeTask> itemList = new ArrayList<>();
        if (files == null) {
            return new ArrayList<>();
        }
        for (File file : files) {
            if (file.isDirectory()) {
                MergeTask item = new MergeTask();
                item.init(file);
                itemList.add(item);
            }
        }
        return itemList;
    }

    public void mergeFiles(ObservableList<MergeTask> tasks) {
        tasks.forEach(App.exec::execute);
    }

}
